<?php
require_once '../setting.php';
header('Content-Type:text/html;charset=UTF-8');

$tag_ctrl = TagsManager::instance(NoteSystem::TAG_TABLE_COL);
$act = @$_POST['act']?:@$_GET['act'];
$id = @$_POST['id']?:@$_GET['id'];   
    switch ($act)
    {
        case 'del_tag':
            $tag_ctrl->del(@$_GET['name']);
            break;
        
        case 'add':
            NoteSystem::add(array(
                'tags' => @$_POST['tags'],//標籤
                'gold' => @$_POST['gold'],//金額
                'subject' => @$_POST['subject'],//標題
                'note' => @$_POST['note'],//備註
            ));
            break;
        
        case 'edit':
            if (@$_POST['id'])
            {
                NoteSystem::edit($id, array(
                    'tags' => @$_POST['tags'],//標籤
                    'gold' => @$_POST['gold'],//金額
                    'subject' => @$_POST['subject'],//標題
                    'note' => @$_POST['note'],//備註
                ));
            }
            $edit_one = NoteSystem::get($id);
            break;
       
       case 'del':
           NoteSystem::del($id);
           break;
       
       default: /* empty */
    }


$search_tag = TagsManager::parse(@$_GET['tags']);
$tags = $tag_ctrl->fetch();
$data = NoteSystem::find($search_tag);
?>
<script src="/js/jquery.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css" />
<style>
    html *{font-family: "微軟正黑體", "Comic Sans MS";}
</style>
<body>
<div class="navbar navbar-inverse">
    <div class="navbar-inner">
        <div class="container">
            <a class="brand" href="#">Freefay</a>
            <ul class="nav">
                <li><a href="/">帳務</a></li>
                <li><a href="/notes.php">雜記</a></li>
            </ul>
        </div>
    </div>
    <form  class="navbar-inner">
        <a class="btn" href="<?php echo $_SERVER['PHP_SELF'];?>">重整</a>
        <button type="submit" class="btn btn-primary">查詢</button>

        <fieldset>
        <?php foreach ($tags as $tag_name):?>
        <label class="btn btn-primary btn-small">
            <input <?php if (in_array($tag_name, $search_tag)):?>checked="checked"<?php endif;?> type="checkbox" name="tags[]" value="<?php echo $tag_name;?>" /> <?php echo $tag_name;?>
            <a class="btn btn-danger btn-mini" onclick="return confirm('確定刪除此標籤?')" href="?act=del_tag&name=<?php echo $tag_name;?>">x</a>
        </label>
        <?php endforeach;?>
        </fieldset>
    </form>
</div>

<div>
    <form method="post" action="?act=<?php echo in_array($act, array('add','edit'))?$act:'add';?>">
        <?php if ( ! empty($id) && $act !== 'del'):?>
            <input type="hidden" name="id" value="<?php echo $id;?>">
        <?php endif;?>

        <fieldset>
            <legend>標籤</legend>
        <?php $_tags = TagsManager::parse(@$edit_one['tags']);?>
        <?php foreach ($tags as $tag_name):?>
        <label class="btn btn-info">
            <input <?php if (in_array($tag_name, $_tags)):?>checked="checked"<?php endif;?> type="checkbox" name="tags[]" value="<?php echo $tag_name;?>" /> <?php echo $tag_name;?>
        </label>
        <?php endforeach;?>
        <hr />
        <label>其他標籤 / <input name="tags[]" /></label>
        <hr />
        </fieldset>

        <label>標題 / <input name="subject" value="<?php echo @$edit_one['subject'];?>" /></label>
        <label>金額 / <input name="gold" value="<?php echo @$edit_one['gold'];?>" /></label>
        <label>備註 / <textarea name="note" class="span6" rows="3"><?php echo @$edit_one['note'];?></textarea></label>
        <div>
            <a class="btn" href="<?php echo $_SERVER['PHP_SELF'];?>">清空</a>
            <button type="submit" class="btn btn-success">儲存</button>
        </div>
    </form>

    <hr />

    <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th class="span1">編號</th>
                <th>功能</th>
                <th>標籤</th>
                <th>標題</th>
                <th>金額</th>
                <th>備註</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($data as $row):?>
            <tr>
                <td><?php echo isset($cnt)?++$cnt:$cnt=1;?></td>
                <td>
                    <a class="btn btn-danger" href="?act=del&id=<?php echo $row['id'];?>" onclick="return confirm('確定刪除<?php echo join('\n',$row);?>?')">刪除</a>
                    <a class="btn btn-primary" href="?act=edit&id=<?php echo $row['id'];?>">編輯</a>
                </td>
                <td><?php echo $row['tags'];?></td>
                <td><?php echo $row['subject'];?></td>
                <td><?php echo $row['gold'];?></td>
                <td><?php echo nl2br($row['note']);?></td>
            </tr>
            <?php endforeach;?>
        </tbody>
    </table>
</div>
</body>